import * as api from '@/api'
import * as types from '../mutation-types'
/**
 * Format array to object
 * @example
 *  input:
 *   [
 *     {
 *       "attendanceId": "yyy",
 *       "attendanceName": "客服部夜班",
 *       "employeeName": "小黑",
 *       "earlyTime": 0,
 *       "lastTime": 41395000,
 *       "earlyTimeLocation": "",
 *       "lastTimeLocation": "长虹科技大厦"
 *     },
 *     {
 *       "attendanceId": "bbb",
 *       "attendanceName": "客服部白班",
 *       "employeeName": "小明",
 *       "earlyTime": 0,
 *      "lastTime": 41395000,
 *      "earlyTimeLocation": "",
 *      "lastTimeLocation": "长虹科技大厦"
 *     }
 *   ]
 *   output:
 *    {
 *       "yyy": {
 *         "name": "客服部夜班",
 *         "list": []
 *       },
 *      "bbb": {
 *         "name": "客服部白班",
 *         "list": []
 *       }
 *    }
 *
 */
const formatAttendanceList = list => {
  let listMap = {}
  list.forEach((value, index) => {
    if (value.attendanceId in listMap) {
      listMap[value.attendanceId].list.push(value)
    } else {
      listMap[value.attendanceId] = {
        name: value.attendanceName,
        list: [ value ]
      }
    }
  })

  return listMap
}

// 初始 state
const state = {
  teamDailyInfo: {          // 团队日报详情信息-需要分页加载
    pageNo: 1,
    teamDailyInfoList: [],
    teamDailyInfoFormattedMap: {}
  },
  teamDailyReport: {},      // 团队日报统计
  teamMonthlyReport: {      // 团队月报统计-需要分页加载
    pageNo: 1,
    teamMonthlyReportList: [],
    teamMonthlyReportFormattedMap: {}
  },
  personalMonthlyReport: [] // 员工月报明细
}

// getters
const getters = {
  getTeamDailyInfo: state => state.teamDailyInfo,
  getTeamDailyReport: state => state.teamDailyReport,
  getTeamMonthlyReport: state => state.teamMonthlyReport, // 需要分页加载
  getPersonalMonthlyReport: state => state.personalMonthlyReport,
  getTeamMonthlyReportFormattedMap: state => state.teamMonthlyReport.teamMonthlyReportFormattedMap,
  getTeamDailyInfoFormattedMap: state => state.teamDailyInfo.teamDailyInfoFormattedMap
}

// actions
const actions = {
  // 团队日报详情
  // 分页加载每页数据默认为20
  fetchTeamDailyInfo ({ commit, state }, { enterId, date, pageNo, pageSize = '20' }) {
    api.queryTeamDailyInfo({ enterId, date, pageNo, pageSize }, json => {
      if (json.code === 'S_OK') {
        let _var = json.var
        if (_var && Object.prototype.toString.call(_var) === '[object Array]') {
          let teamDailyInfoList = JSON.parse(JSON.stringify(state.teamDailyInfo.teamDailyInfoList)) // 深度拷贝
          let teamDailyInfoFormattedMap = {}
          if (Number(pageNo) === 1) {
            if (_var.length > 0) {
              teamDailyInfoList = _var
              teamDailyInfoFormattedMap = formatAttendanceList(_var)
            } else {
              pageNo = '0'  // 没有下一页了
              teamDailyInfoList = []
              teamDailyInfoFormattedMap = {}
            }
          } else {
            if (_var.length === 0) {
              pageNo = '0'
            }
            teamDailyInfoList.push(..._var)
            teamDailyInfoFormattedMap = formatAttendanceList(teamDailyInfoList)
          }
          commit(types.FETCH_TEAM_DAILY_INFO, { teamDailyInfoList, teamDailyInfoFormattedMap, pageNo })
        }
      }
    })
  },
  // 团队日报统计
  fetchTeamDailyReport ({ commit, state }, { enterId, queryDay }) {
    api.queryTeamDailyReport({ enterId, queryDay }, json => {
      if (json.code === 'S_OK') {
        let _var = json.var
        commit(types.FETCH_TEAM_DAILY_REPORT, { teamDailyReport: _var })
      }
    })
  },
  // 团队月报统计
  // 分页加载每页数据默认为20
  fetchTeamMonthlyReport ({ commit, state }, { enterId, queryMonth, pageNo, pageSize = '20' }) {
    api.queryTeamMonthlyReport({ enterId, queryMonth, pageNo, pageSize }, json => {
      if (json.code === 'S_OK') {
        let _var = json.var
        if (_var && Object.prototype.toString.call(_var) === '[object Array]') {
          let teamMonthlyReportList = JSON.parse(JSON.stringify(state.teamMonthlyReport.teamMonthlyReportList)) // 深度拷贝
          let teamMonthlyReportFormattedMap = {}
          if (Number(pageNo) === 1) {
            if (_var.length > 0) {
              teamMonthlyReportList = _var         // 第1页
              teamMonthlyReportFormattedMap = formatAttendanceList(_var)
            } else {
              pageNo = '0' // 没有下一页了
              teamMonthlyReportList = []          // 第1页
              teamMonthlyReportFormattedMap = {}
            }
          } else {
            if (_var.length === 0) {
              pageNo = '0'
            }
            teamMonthlyReportList.push(..._var)  // 分页
            teamMonthlyReportFormattedMap = formatAttendanceList(teamMonthlyReportList)
          }
          commit(types.FETCH_TEAM_MONTHLY_REPORT, { teamMonthlyReportList, teamMonthlyReportFormattedMap, pageNo })
        }
      }
    })
  },
  // 个人月报统计
  fetchPersonalMonthlyReport ({ commit, state }, { enterId, uid, queryMonth }) {
    api.queryPersonalMonthlyReport({ enterId, uid, queryMonth }, json => {
      if (json.code === 'S_OK') {
        let _var = json.var
        if (_var && Object.prototype.toString.call(_var) === '[object Array]') {
          commit(types.FETCH_PERSONAL_MONTHLY_REPORT, { personalMonthlyReport: _var })
        }
      }
    })
  }
}

// mutations
const mutations = {
  [types.FETCH_TEAM_DAILY_INFO] (state, { teamDailyInfoList, teamDailyInfoFormattedMap, pageNo }) {
    let _teamDailyInfo = {}
    _teamDailyInfo.teamDailyInfoList = teamDailyInfoList
    _teamDailyInfo.teamDailyInfoFormattedMap = teamDailyInfoFormattedMap
    _teamDailyInfo.pageNo = pageNo
    state.teamDailyInfo = _teamDailyInfo
  },
  [types.FETCH_TEAM_DAILY_REPORT] (state, { teamDailyReport }) {
    state.teamDailyReport = teamDailyReport
  },
  [types.FETCH_TEAM_MONTHLY_REPORT] (sate, { teamMonthlyReportList, teamMonthlyReportFormattedMap, pageNo }) {
    let _teamMonthlyReport = {}
    _teamMonthlyReport.pageNo = pageNo
    _teamMonthlyReport.teamMonthlyReportList = teamMonthlyReportList
    _teamMonthlyReport.teamMonthlyReportFormattedMap = teamMonthlyReportFormattedMap
    state.teamMonthlyReport = _teamMonthlyReport
  },
  [types.FETCH_PERSONAL_MONTHLY_REPORT] (state, { personalMonthlyReport }) {
    state.personalMonthlyReport = personalMonthlyReport
  }
}

// export utils
export default {
  state,
  getters,
  actions,
  mutations
}
